package view.CURD;

import library.controller.FrameListener;
import library.controller.JTextFieldListener;
import view.info.OverviewDialog;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class SelectDialog extends JDialog {
    public SelectDialog(OverviewDialog owner,String title) {
        super(owner);
        this.listener = owner.getListener();
        this.setTitle(title);
        this.setVisible(true);
        initComponents();
    }
    public SelectDialog(OverviewUserDialog owner,String title) {
        super(owner);
        this.listener = owner.getListener();
        this.setTitle(title);
        this.setVisible(true);
        initComponents();
    }
    public SelectDialog(BookViewDialog owner,String title) {
        super(owner);
        this.listener = owner.getListener();
        this.setTitle(title);
        this.setVisible(true);
        initComponents();
    }
    private void initComponents() {
        dialogPane = new JPanel();
        okButton = new JButton();
        textField = new JTextField();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        this.setBounds(0,0,500,250);
        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.
                    border.EmptyBorder(0, 0, 0, 0), "", javax.swing.border.TitledBorder.CENTER
                    , javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dia\u006cog", java.awt.Font
                    .BOLD, 12), java.awt.Color.red), dialogPane.getBorder()));
            dialogPane.addPropertyChangeListener(
                    new java.beans.PropertyChangeListener() {
                        @Override
                        public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if ("bord\u0065r"
                                    .equals(e.getPropertyName())) throw new RuntimeException();
                        }
                    });
            dialogPane.setLayout(null);

            //---- okButton ----
            okButton.setText("\u67e5\u8be2");
            okButton.setBounds(130, 155, 115, 30);
            okButton.addActionListener(listener);
            if ("查询借阅信息".equals(this.getTitle())){
                okButton.setActionCommand("SelectBorrow");
                dialogPane.add(okButton);
            }
            else if ("查询图书".equals(this.getTitle())){
                okButton.setActionCommand("SelectBook");
                dialogPane.add(okButton);
            }
            else if ("查询用户".equals(this.getTitle())){
                okButton.setActionCommand("SelectUser");
                dialogPane.add(okButton);
            }
            textField.addFocusListener(new JTextFieldListener(textField, "该搜索为模糊搜索，可以不用准确输入信息."));
            dialogPane.add(textField);
            textField.setBounds(40, 85, 300, 30);
            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < dialogPane.getComponentCount(); i++) {
                    Rectangle bounds = dialogPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialogPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialogPane.setMinimumSize(preferredSize);
                dialogPane.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
    }
    public JTextField getTextField(){
        return this.textField;
    }
    private JPanel dialogPane;
    private JButton okButton;
    private JTextField textField;
    private FrameListener listener;
}
